<?php
/**
 * 主题管理 - JBlog
 *
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_theme.php 554 2010-07-22 09:37:13Z lisijie86 $
*/

!defined('IN_ADMIN') && exit('Access Denied!');

if ( !is_founder() ) { prompt(__('对不起，你没有权限执行这项操作。')); }

empty($ac) && $ac = 'themes';

$tabon[$ac] = ' class="on"';

if ($ac == 'themes') {

	$theme_root = JBLOG_ROOT.'themes/';

	$result = read_dir($theme_root);

	$cur_theme = config('theme');

	$themes = array();
	foreach ($result['dirs'] as $dir) {
		$dir = str_replace($theme_root, '', $dir);
		if ( $dir{0} == '.' ) continue;
		$themes[$dir]['directory'] = $dir;
		$themes[$dir]['path'] = 'themes/'.$dir.'/';
		$infofile = $themes[$dir]['path'].'info.xml';
		if ( file_exists($infofile) ) {
			$info = parse_theme_info($infofile);
			$themes[$dir] = array_merge($themes[$dir], $info);
		}
	}

} elseif ( $ac == 'settheme' ) {

	$theme = check_str(getGP('theme'));
	if (is_dir(JBLOG_ROOT.'themes/'.$theme)) {
		$db->insert('config', array('name'=>'theme','value'=>$theme,'type'=>'base'), TRUE);
		recache('config');
	}
	redirect('admin.php?mod=theme');

} elseif ( $ac == 'widgets' ) {

	$activation_widgets = $db->result("SELECT `value` FROM ".tname('config')." WHERE `name` = 'widgets'");
	if ( $activation_widgets ) {
		$activation_widgets = unserialize($activation_widgets);
	} else {
		$activation_widgets = array();
	}
	ksort($activation_widgets);
	$widget_root = JBLOG_ROOT.'plugin'.DS.'widget';
	$result = read_dir($widget_root);
	$result = $result['files'];
	$widgets = array();
	foreach ( $result as $file ) {
		$name = basename($file, '.php');
		if ( !in_array($name, $activation_widgets) ) {
			$widgets[$name] = parse_plugin_info($file);
		}
	}

} elseif ( $ac == 'addwidget' ) {

	initGP('id');
	$id = str_replace('.','',$id);
	if ( is_file(JBLOG_ROOT.'plugin'.DS.'widget'.DS.$id.'.php') ) {
		$activation_widgets = $db->result("SELECT `value` FROM ".tname('config')." WHERE `name` = 'widgets'");
		if ( $activation_widgets ) {
			$activation_widgets = unserialize($activation_widgets);
		} else {
			$activation_widgets = array();
		}
		if ( ! in_array($id, $activation_widgets) ) {
			$activation_widgets[] = $id;
			$activation_widgets = serialize($activation_widgets);
			$db->insert('config', array('name'=>'widgets','value'=>$activation_widgets), true);
			recache('config');
		}
	}
	redirect('?mod=theme&ac=widgets');

} elseif ( $ac == 'removewidget' ) {

	initGP('id');
	$id = str_replace('.','',$id);
	$activation_widgets = $db->result("SELECT `value` FROM ".tname('config')." WHERE `name` = 'widgets'");
	if ( $activation_widgets ) {
		$activation_widgets = unserialize($activation_widgets);
		if ( in_array($id, $activation_widgets) ) {
			foreach ( $activation_widgets as $key => $val ) {
				if ( $val == $id ) {
					unset($activation_widgets[$key]);
					break;
				}
			}
			$activation_widgets = serialize($activation_widgets);
			$db->insert('config', array('name'=>'widgets','value'=>$activation_widgets), true);
			recache('config');
		}
	}
	redirect('?mod=theme&ac=widgets');

} elseif ( $ac == 'savewidget' ) {

	$name = $_POST['name'];
	$widget = Widget::factory($name);
	unset($_POST['name']);
	$updata = $widget->update($_POST);
	$widget->set_setting($updata);
	
	do_action('save_widget');
	
	ajax_out(array('status'=>'success'));

}

include admin_tpl('theme');
?>